# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Contributor: Michael Zhou <zhoumichaely@gmail.com>
# Maintainer: Daniel Sabogal <dsabogalcc@gmail.com>
pkgname=mupdf
pkgver=1.20.0
pkgrel=0
pkgdesc="Lightweight PDF and XPS viewer"
url="https://mupdf.com"
arch="all"
license="AGPL-3.0-or-later"
makedepends="
	freetype-dev
	glu-dev
	harfbuzz-dev
	jbig2dec-dev
	jpeg-dev
	libx11-dev
	libxext-dev
	libxi-dev
	libxrandr-dev
	mesa-dev
	openjpeg-dev
	readline-dev
	zlib-dev
	"

subpackages="
	$pkgname-doc
	$pkgname-dev
	$pkgname-x11:_x11
	$pkgname-gl:_gl
	$pkgname-tools:_tools
	"
options="!check"
source="https://mupdf.com/downloads/archive/mupdf-$pkgver-source.tar.lz
	shared-lib.patch"

# FIXME: shared linking of /usr/lib/libmupdf.so.0

# secfixes:
#   1.18.0-r1:
#     - CVE-2021-3407
#   1.17.0-r3:
#     - CVE-2020-26519
#   1.13-r0:
#     - CVE-2018-1000051
#     - CVE-2018-6544
#     - CVE-2018-6192
#     - CVE-2018-6187
#     - CVE-2018-5686
#     - CVE-2017-17858
#   1.11-r1:
#     - CVE-2017-6060
#   1.10a-r2:
#     - CVE-2017-5991
#   1.10a-r1:
#     - CVE-2017-5896

builddir="$srcdir/$pkgname-$pkgver-source"
prepare() {
	default_prepare

	local library=
	for library in curl freetype harfbuzz jbig2dec \
			libjpeg openjpeg zlib; do
		rm -r thirdparty/"$library"
	done
}

build() {
	make shared=yes USE_SYSTEM_LIBS=yes USE_SYSTEM_GUMBO=no USE_SYSTEM_GLUT=no prefix=/usr CURL_LIBS='-lcurl -lpthread' build=release libs apps
}

package() {
	make shared=yes USE_SYSTEM_LIBS=yes USE_SYSTEM_GUMBO=no USE_SYSTEM_GLUT=no prefix=/usr DESTDIR="$pkgdir" install
	cd "$pkgdir"/usr/lib/
	ln -s libmupdf.so libmupdf.so.0
}

_x11() {
	pkgdesc="$pkgdesc with X11 backend"
	depends=""

	mkdir -p "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/bin/mupdf-x11 \
		"$subpkgdir"/usr/bin/
	ln -s /usr/bin/mupdf-x11 "$subpkgdir"/usr/bin/mupdf
}

_gl() {
	pkgdesc="$pkgdesc with OpenGL backend"
	depends=""

	mkdir -p "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/bin/mupdf-gl \
		"$subpkgdir"/usr/bin/
}

_tools() {
	pkgdesc="Tools for a lightweight PDF and XPS viewer"
	depends=""

	mkdir -p "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/bin/mutool \
		"$pkgdir"/usr/bin/muraster \
		"$subpkgdir"/usr/bin/
}

sha512sums="
c5920addb249753aab1e4eac197edca6a5e2bd4dd2b472a629994426048993e65c3f4d34ccc9620b7b6bcd917fb06f36017c02afc2ec060c90087a786d9d5d4c  mupdf-1.20.0-source.tar.lz
84778a62fb1f504887324647312b747275c334bab579baa66a9d9c00e2fa8d78467ee3d7e1afdc47ce0887426e447a5fae48ba903c7c8826d5a366d6e97f1202  shared-lib.patch
"
